import time
from mrjob.job import MRJob
from mrjob.protocol import TextProtocol


class TimeConvert(MRJob):

    OUTPUT_PROTOCOL = TextProtocol

    def mapper(self, _, line):
        lineStr = line.strip()
        lineArray = lineStr.split(',')
        if len(lineArray) == 5:
            now_stamp = int(lineArray[4])

            start_time = "2017-11-25 0:0:0"
            end_time = "2017-12-04 0:0:0"

            start_arr = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
            end_arr = time.strptime(end_time, "%Y-%m-%d %H:%M:%S")

            start_stamp = int(time.mktime(start_arr))
            end_stamp = int(time.mktime(end_arr))

            if start_stamp <= now_stamp <= end_stamp:
                time_tuple = time.localtime(now_stamp)
                date_time = time.strftime(",%Y-%m-%d,%H:%M:%S", time_tuple)
                yield ','.join(lineArray[:-1]) + date_time, 1

    def combiner(self, key, value):
        yield key, sum(value)

    def reducer(self, key, value):
        yield key, None


if __name__ == '__main__':
    TimeConvert.run()
